package edu.gdkm.listdemo;

import java.io.*;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.Scanner;

public class SetTest {
    public static void main(String[] args) throws IOException {
        //Set是一种无序集合，所以不能使用下标获取元素
        //Set中元素不能重复
        Set<String> set = new HashSet();
        File file = new File("./data/scores.csv");
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;
        while ((line = br.readLine()) != null) {
            set.add(line);                     //set.add()将元素添加到set
        }
        br.close();
        System.out.println("一共有"+set.size()+"条记录");
        //按学号查找同学的所有信息
        while(true){
            System.out.println("请输入要查找的学号：");
            Scanner scanner = new Scanner(System.in);
            String id = scanner.next();
            boolean flag = false;
            long time1 = System.currentTimeMillis();
            //set遍历使用先序列化，通过iterator的hasNext判断是否有下一个元素，通过next方法获取下一个元素
            Iterator<String> iterator=set.iterator();
            while (iterator.hasNext()) {
                String s=iterator.next();
                if (s.contains(id)) {
                    
                    flag = true;
                    System.out.println(s);
                    break;
                }

            }
            if(flag){
                System.out.println("查找成功，已输出");
            }else{
                System.out.println("查找失败，请重新输入");
            }
            long time2= System.currentTimeMillis();
            System.out.println("查询时间：" +(time2-time1)+"ms");
        }
    }

}
